﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;

namespace LanguageConfigurationSolution
{
    public class ManageLanguageConfiguration : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        protected LanguageConfigurationListSelector LCListSelector1;
        protected DropDownList DdlDefaultSubWeb;
        protected Label lblStatus;
        protected CheckBoxList cblRootWebSubWebs;

        private DataTable dtRootWebSubWebs;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    SPSite site = new SPSite(SPContext.Current.Site.ID);
                    string defaultsubweb = site.RootWeb.Properties[RootLandingPage.DEFAULTSUBWEBPROPERTYNAME];
                    foreach (SPWeb web in site.RootWeb.Webs)
                    {
                        ListItem li = new ListItem(web.Name, web.Name);
                        if (defaultsubweb != null && string.Compare(web.Name, defaultsubweb, true) == 0)
                        {
                            li.Selected = true;
                        }
                        DdlDefaultSubWeb.Items.Add(li);
                    }
                }
                );
            }
        }

        protected override void CreateChildControls()
        {
            dtRootWebSubWebs = new DataTable();
            DataColumn dcId = dtRootWebSubWebs.Columns.Add("Id", typeof(Guid));
            DataColumn dcName = dtRootWebSubWebs.Columns.Add("Name", typeof(string));
            DataColumn dcServerRelativeUrl = dtRootWebSubWebs.Columns.Add("ServerRelativeUrl", typeof(string));

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using(SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    foreach (SPWeb web in site.RootWeb.Webs)
                    {
                        DataRow row = dtRootWebSubWebs.NewRow();
                        row[dcId] = web.ID;
                        row[dcName] = web.Name;
                        row[dcServerRelativeUrl] = web.ServerRelativeUrl;
                        dtRootWebSubWebs.Rows.Add(row);
                    }
                }
            }
            );
            cblRootWebSubWebs.DataSource = dtRootWebSubWebs;
            cblRootWebSubWebs.DataBind();

            base.CreateChildControls();
        }

        public void CheckLanguageSubWeb(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                foreach (ListItem l in ((CheckBoxList)sender).Items)
                {
                    using (SPWeb web = site.OpenWeb(new Guid(l.Value)))
                    {
                        if (web.Properties.ContainsKey("LanguageSubWeb"))
                        {
                            l.Selected = true;
                        }
                    }
                }
            }
        }
        

        public void SaveClick(object sender, EventArgs e)
        {
            EnsureChildControls();            
            if (SPContext.Current != null)
            {
                using (SPSite site = SPContext.Current.Site)
                {
                    using (SPWeb rootweb = site.OpenWeb(site.RootWeb.ID))
                    {
                        rootweb.Properties["LanguageConfigurationList"] = LCListSelector1.LanguageConfigurationList;
                        rootweb.Properties[RootLandingPage.DEFAULTSUBWEBPROPERTYNAME] = DdlDefaultSubWeb.SelectedValue;
                        rootweb.Properties.Update();
                    }

                    foreach (ListItem li in cblRootWebSubWebs.Items)
                    {
                        using (SPWeb web = site.OpenWeb(new Guid(li.Value)))
                        {
                            if (li.Selected)
                            {
                                web.Properties["LanguageSubWeb"] = true.ToString();
                            }
                            else
                            {
                                web.Properties["LanguageSubWeb"] = null;
                            }
                            web.Properties.Update();
                        }
                    }
                }

                lblStatus.Text = "Saved...";
            }

        }
    }
}
